<?php

namespace app\api\model\gifts;
use app\common\model\Gifts as GiftsModel;
/**
 * 商品模型
 * Class Goods
 * @package app\store\model
 */
class Gifts extends GiftsModel
{


    /**
     * 首页爆品礼列表
     * @param $param
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function buyList($param,$user = false,$only_receive = true)
    {
        $time = time();
        $page = isset($param['page']) && is_numeric($param['page']) ? intval($param['page']) : 1;
        $limit = 10;
        $offset = ($page - 1) * $limit;

        // 构造查询条件
        $this->where('is_delete', '=', 0)
            ->where('gifts_status','=',10);

        if ($only_receive) {
            $this->where('gifts_stock_num','>','0')
                ->where("((`end_time` + 86400) > {$time} )");
        }

        $allList = $this->with(['image'])
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->field(['gifts_id,
                    gifts_title,
                    gifts_type,
                    image_id,
                    gifts_line_price,
                    gifts_price,
                    gifts_discription,
                    start_time,
                    end_time,
                    receive_num'
            ])
            ->limit($offset, $limit)
            ->select();

        // 获取用户已购买的爆品礼
        if ($user !== false) {
            $UserGiftsModel = new UserGifts;
            $userGiftsIds = $UserGiftsModel->getBuyGiftsIds($user['user_id']);
            foreach ($allList as $key => $item) {
                $allList[$key]['is_receive'] = in_array($item['gifts_id'], $userGiftsIds);
            }
        }

        return $allList;
    }


    /**
     * 微股客爆品礼列表
     * @param $param
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function keList($param,$user = false,$only_receive = true)
    {
        $time = time();
        $page = isset($param['page']) && is_numeric($param['page']) ? intval($param['page']) : 1;
        $limit = 10;
        $offset = ($page - 1) * $limit;

        // 构造查询条件
        $this->where('is_delete', '=', 0)
            ->where('gifts_status','=',10);

        if ($only_receive) {
            $this->where('gifts_stock_num','>','0')
                ->where("((`end_time` + 86400) > {$time} )");
        }

        $allList = $this->with(['image'])
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->field(['gifts_id,
                    gifts_title,
                    gifts_type,
                    image_id,
                    gifts_line_price,
                    gifts_price,
                    gifts_discription,
                    start_time,
                    end_time,
                    receive_num'
            ])
            ->limit($offset, $limit)
            ->select();
        // 获取微股客已领取的爆品礼
        if ($user !== false) {
            $UserGiftsModel = new UserGifts;
            $userGiftsIds = $UserGiftsModel->getKeGiftsIds($user['user_id']);
            foreach ($allList as $key => $item) {
                $allList[$key]['is_receive'] = in_array($item['gifts_id'], $userGiftsIds);
            }
        }

        return $allList;
    }


    /**
     * 验证优惠券是否可领取
     * @return bool
     */
    public function checkReceive()
    {

        if ($this['gifts_status']['value']==20) {
            $this->error = '爆品礼已下架';
            return false;
        }

        if ($this['is_delete']==1) {
            $this->error = '爆品礼已下架';
            return false;
        }
        if ($this['gifts_stock_num']<1) {
            $this->error = '爆品礼已发完';
            return false;
        }

        if (($this['end_time']['value'] + 86400) < time()) {
            $this->error = '爆品礼已过期';
            return false;
        }
        return true;
    }


    /**
     * 验证优惠券是否可领取
     * @return bool
     */
    public function checkCanBuy()
    {

        if ($this['gifts_status']['value']==20) {
            $this->error = '爆品礼已下架';
            return false;
        }

        if ($this['is_delete']==1) {
            $this->error = '爆品礼已下架';
            return false;
        }
        if ($this['gifts_stock_num']<1) {
            $this->error = '爆品礼已发完';
            return false;
        }

        if (($this['end_time']['value'] + 86400) < time()) {
            $this->error = '爆品礼已过期';
            return false;
        }
        return true;
    }

    /**
     * 累计已领取数量
     * @return int|true
     * @throws \think\Exception
     */
    public function setIncReceiveNum()
    {
        $this->setInc('receive_num');
        //$this->setDec('gifts_stock_num');
    }

}
